
import {Cell} from "./Cell.js";
import {GameObjects} from "./GameObjects.js";

export class Snake extends GameObjects{
    constructor(info,gamemap) {
        super();
        // this.info=info;
        this.gamemap=gamemap;
        this.id=info.id;
        this.color=info.color;
        this.cells=[new Cell(info.r,info.c)];//存放蛇头
        this.next_cell=null; //目标位置

        this.speed=1;
        this.direction=-1;
        //用这个去判断指令，0，1，2，3分别表示上右下左


        this.dc=[-1,0,1,0];
        this.dr=[0,1,0,-1];

        this.status="idle";//idle表示闲置u后者说是静止的意思，move表示正在动，die表示死亡
        this.step=0;//回合数


        this.eps=1e-2;
        this.eye_direction=0;
        if(this.id===0){
            this.eye_direction=2;
        }

        this.eye_dx=[
            //蛇蛇眼睛的偏移量
            [-1,1],
            [1,1],
            [1,-1],
            [-1,-1],
        ];
        this.eye_dy=[
            [-1,-1],
            [-1,1],
            [1,1],
            [-1,1],
        ]
    }
    start(){

    }
    set_direction(d){
        this.direction=d;

        this.check_tail_licreasing()

    }
    check_tail_licreasing(){
        if(this.step<=10)return true;
        if(this.step%3===1)return true;

        return false;
    }
    update(){
        if(this.status==="move")
        this.update_move()
        this.render()

    }
    next_step(){
        const d=this.direction;
        this.next_cell=new Cell(this.cells[0].r+this.dr[d],this.cells[0].c+this.dc[d]);
        this.eye_direction=1;
        //清空
        this.direction=-1;
        this.status="move";
        this.step++;




        const k=this.cells.length;
        for(let i=k;i>0;i--){
            this.cells[i]=JSON.parse(JSON.stringify(this.cells[i-1]));
        }
        if(!this.gamemap.check_valid(this.next_cell)){
            this.status="die"
        }

    }

    update_move(){
        // this.cells[0].x+=this.speed*this.delta_time/1000;

        const move_distance=this.speed*this.delta_time/1000;

        const dx=this.next_cell.x-this.cells[0].x;
        const dy=this.next_cell.y-this.cells[0].y;
        const distance=Math.sqrt(dx*dx+dy*dy);
        // console.log("lzrgege")
        if(distance<this.eps){
            //走到目标点了
            this.cells[0]=this.next_cell;//添加了一个新的蛇蛇头
            this.next_cell=null

            this.status="idle";
            //走完了，停下来

            if(!this.check_tail_licreasing()){
                //这个意思就是蛇蛇不变长

                this.cells.pop();
            }

        }
        else{
            const move_distance=this.speed*this.delta_time/1000;
            this.cells[0].x+=move_distance*dx/distance
            this.cells[0].y+=move_distance*dy/distance



            if(!this.check_tail_licreasing()){
                const k=this.cells.length;
                const tail=this.cells[k-1],tail_target=this.cells[k-2];
                const tail_dx=tail_target.x-tail.x;
                const tail_dy=tail_target.y-tail.y;

                tail.x+=move_distance*tail_dx/distance;
                tail.y+=move_distance*tail_dy/distance;

            }
        }

    }
    render(){
        const L=this.gamemap.L;
        const ctx=this.gamemap.ctx;

        ctx.fillStyle=this.color;

        if(this.status==="die"){
            ctx.fillStyle="white"
        }
        // ctx.beginPath();
        for(const cell of this.cells){
            ctx.beginPath();
            ctx.arc(cell.x*L,cell.y*L,L/2,0,Math.PI*2);
            ctx.fill();

        }
        for(let i=1;i<this.cells.length;i++){
            const a=this.cells[i-1],b=this.cells[i];
            if(Math.abs(a.x-b.x)<this.eps&&Math.abs(a.y-b.y)<this.eps){
                continue;
            }
            if(Math.abs(a.x-b.x)<this.eps){
                ctx.fillRect((a.x-0.5)*L,Math.min(a.y,b.y)*L,L,Math.abs(a.y-b.y)*L);
            }
            else{
                ctx.fillRect(Math.min(b.x,a.x)*L,(a.y-0.5)*L,Math.abs(a.x-b.x)*L,L);
            }
        }

        ctx.fillStyle="black";
        for(let i=0;i<2;i++){

            const eye_x=(this.cells[0].x+this.eye_dx[this.eye_direction][i]*0.25)*L;
            const eye_y=(this.cells[0].y+this.eye_dy[this.eye_direction][i]*0.25)*L;

            ctx.beginPath();
            ctx.arc(eye_x,eye_y,0.1*L,0,Math.PI*2);
            ctx.fill();
            // console.log(1)
        }
    }
}